import os



import pandas as pd
import re
import json
import time
import requests
from io import StringIO
import datetime



def HuoQuZhangTingBan(ti,year):
    print(ti)
    start_date = str(int(ti))
    year = start_date[0:4]
    month = start_date[4:6]
    day = start_date[6:8]
    input_date = f"{year}-{month}-{day}"
    date_obj = datetime.datetime.strptime(input_date, "%Y-%m-%d")
    # 使用date.weekday()方法获取星期几(0-6，0表示星期一)
    weekday = date_obj.weekday()
    print(weekday)
    print(type(weekday))
    if weekday in [5,6]:
        pass
    else:
        zhangtingbanurl=f'https://push2ex.eastmoney.com/getTopicZTPool?cb=callbackdata8153488&ut=7eea3edcaed734bea9cbfc24409ed989&dpt=wz.ztzt&Pageindex=0&pagesize=100&sort=fbt%3Aasc&date={ti}&_=1720532501294'
       # 炸板
        zabanurl=f'https://push2ex.eastmoney.com/getTopicZBPool?cb=callbackdata5695439&ut=7eea3edcaed734bea9cbfc24409ed989&dpt=wz.ztzt&Pageindex=0&pagesize=100&sort=fbt%3Aasc&date={ti}&_=1720616816328'
        def spide_stock(url, sleep_time=5, max_try_number=5):
            for i in range(max_try_number):
                responses = requests.get(url, timeout=30)
                # 请求成功
                if responses.status_code == 200:
                    return responses
                else:
                    print('连接失败', responses)
                    time.sleep(sleep_time)
                    i += 1
        # 涨停板
        content = spide_stock(zhangtingbanurl)  # 抓取数据，去掉空格
        # 分析数据
        json_data = re.findall(r'\{.*}', content.text)[0]  # 提取JSON数据
        # 将数据解析为字典类型
        data_dict = json.loads(json_data)
        is_data_exist = data_dict['data']

        # 炸板
        content2 = spide_stock(zabanurl)
        json_data2 = re.findall(r'\{.*}', content2.text)[0]  # 提取JSON数据
        # 将数据解析为字典类型
        data_dict2 = json.loads(json_data2)
        is_data_exist2 = data_dict2['data']
        pool_zabans = is_data_exist2['pool']
        print(pool_zabans)
        pools = is_data_exist['pool']
        file=f"C:/Users/Administrator/Desktop/fupan/zhangtingbanshouban/{year}/{ti}.txt"
        if not os.path.exists(file):
            with open(file, mode="a+") as f:
                num=len(pools)
                f.write(f"涨停板,个数：{num}\n")
                for pool in pools:
                    print(pool)
                    n=pool['n']
                    c=pool['c']
                    hybk=pool['hybk']
                    f.write(f"{c},{n},{hybk}" + "\n")
                num_zabans=len(pool_zabans)
                f.write(f"\n炸板,个数：{num_zabans}\n")
                for pool_zaban in pool_zabans:
                    print(pool)
                    n=pool_zaban['n']
                    c=pool_zaban['c']
                    hybk=pool_zaban['hybk']
                    f.write(f"{c},{n},{hybk}" + "\n")
            return 1
        else:
            return 0


if __name__ == '__main__':

    now =datetime.datetime.now()
    year=now.year
    ti = time.strftime("%Y%m%d", time.localtime())
    # year=2024
    # ti='20240723'
    # 涨停板
    HuoQuZhangTingBan(ti,year)

